﻿#ifndef _COMMON_H_
#define _COMMON_H_


#include <stdio.h>
#include <string.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h> 
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <iostream>
#include <thread>
#include <memory>
#include <functional>
// #include "log4z.h"
#include "DataCache.h"
#include <unistd.h>
#include <fcntl.h>
#include <mutex>
#include <sys/eventfd.h>
#include "ethernet.h"
#include "SLT_log.h"


#define LOGFMT_TRACE(id, fmt, ...)  SLT_LOG(id,SLT_TRACE,fmt, ##__VA_ARGS__)
#define LOGFMT_DEBUG(id, fmt, ...)  SLT_LOG(id,SLT_DEBUG,fmt, ##__VA_ARGS__)
#define LOGFMT_INFO(id, fmt, ...)  SLT_LOG(id,SLT_INFO,fmt, ##__VA_ARGS__)
#define LOGFMT_WARN(id, fmt, ...)  SLT_LOG(id,SLT_WARM,fmt, ##__VA_ARGS__)
#define LOGFMT_ERROR(id, fmt, ...)  SLT_LOG(id,SLT_ERROR,fmt, ##__VA_ARGS__)
#define LOGFMT_ALARM(id, fmt, ...)  SLT_LOG(id,SLT_ERROR,fmt, ##__VA_ARGS__)
#define LOGFMT_FATAL(id, fmt, ...)  SLT_LOG(id,SLT_ERROR,fmt, ##__VA_ARGS__)
#define LOGFMTT( fmt, ...) LOGFMT_TRACE(0, fmt,  ##__VA_ARGS__)
#define LOGFMTD( fmt, ...) LOGFMT_DEBUG(0, fmt,  ##__VA_ARGS__)
#define LOGFMTI( fmt, ...) LOGFMT_INFO(0, fmt,  ##__VA_ARGS__)
#define LOGFMTW( fmt, ...) LOGFMT_WARN(0, fmt,  ##__VA_ARGS__)
#define LOGFMTE( fmt, ...) LOGFMT_ERROR(0, fmt,  ##__VA_ARGS__)
#define LOGFMTA( fmt, ...) LOGFMT_ALARM(0, fmt,  ##__VA_ARGS__)
#define LOGFMTF( fmt, ...) LOGFMT_FATAL(0, fmt,  ##__VA_ARGS__)

#define     ALL_PACKAGE(data, packnum)    ((((uint64_t)0xFFFFFFFFFFFFFFFF)>>(64 - (packnum))) == (data))


extern void dump_payload(void* _p, int len);
extern int GetIfname(char* name, char* p);
extern std::string GetIfByMac(const std::string& macStr);

extern uint64_t ntohll(uint64_t val);

extern uint64_t htonll(uint64_t val);


// using namespace zsummer::log4z;

typedef  std::lock_guard<std::mutex>  locker;

enum Type : unsigned short
{
    TYPE_BASE_LAN                   = 0xDDDE,
    TYPE_LAN_DATA                   = 0x0201,  /* 非密数据 */
    TYPE_LAN_ARP                    = 0x0202,  /* arp寻址  */
    TYPE_LAN_IN_ENRYPT_DATA         = 0x0203,  /* 内部加密数据 */
    TYPE_LAN_IN_DATA                = 0x0204,  /* 内部非密数据 */
    TYPE_LAN_IN_DECRYPT_ERR         = 0x0205,  /* 内部解密失败数据 */
    TYPE_ERROR                      = 0xFFFF,
};

enum RawType
{
    /*[rxz]读取网卡数据的方式*/
    NETMAP = 1,
    QTXSOCKET,
    PCAP,
};

#pragma pack(push, 1)
struct NetPackHead 
{
    ether_header    m_header;
    uint16_t        m_base_cmd;
    uint16_t        m_cmd;

    uint64_t        m_task_id;
    uint16_t        m_curframe;
    uint16_t        m_allframe;

    uint16_t        m_data_len;

    void toHost();
    void toNet();

    //uint16_t        m_mtu_size;
};
#pragma pack(pop)

#pragma pack(push, 1)
struct ArpPack 
{
    uint8_t     m_dst_mac[6];       //目的mac地址
    uint8_t     m_src_mac[6];       //源mac地址
    uint16_t    m_type;             //
    uint16_t    m_hard_type;
    uint16_t    m_protocol_type;
    uint8_t     m_hard_len;
    uint8_t     m_protocol_len;
    uint16_t    m_op;
    uint8_t     m_src[6];           //发送端mac地址
    uint8_t     m_src_ip[4];        //发送端IP地址
    uint8_t     m_dst[6];           //目的端mac地址
    uint8_t     m_dst_ip[4];        //目的端IP地址
};
#pragma pack(pop)

#endif